﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace GalaxyPresident
{
    public abstract class Planet : BBB
    {
        public int NPLANET = 10;
        SolarSystem solarSystem;

        public SolarSystem SolarSystem
        {
            get { return solarSystem; }
            set { solarSystem = value; }
        }
        protected List<Moon> _moons = new List<Moon>();
        protected List<Unit> _units = new List<Unit>();
        protected string name;
        int patternId;
        float patternRotate;

        protected Color landColor;
        protected Color atmospherColor;

        protected float totalHappiness;
        protected float totalEnvironment;
        protected float totalPopulation;
        protected float totalEconomic;

        protected float energy;
        protected float meterial;
        public int PatternId
        {
            get { return patternId; }
            set { patternId = value; }
        }
        public float PatternRotate
        {
            get { return patternRotate; }
            set { patternRotate = value; }
        }
        public float Energy
        {
            get { return energy; }
            set { energy = value; }
        }

        public float Meterial
        {
            get { return meterial; }
            set { meterial = value; }
        }

        protected Team _teamOwner;

        public Team TeamOwner
        {
            get { return _teamOwner; }
            set { _teamOwner = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<Unit> Units
        {
            get { return _units; }
            set { _units = value; }
        }

        public void AddToLayer(VisibleGameEntity entity)
        {
            ViewLayer.AddChild(entity);
        }
        public void AddToLayer(List<VisibleGameEntity> entitys)
        {
            ViewLayer.AddChilds(entitys);
        }
        public List<Moon> Moons
        {
            get { return _moons; }
            set { _moons = value; }
        }
        public Color LandColor
        {
            get { return landColor; }
            set { landColor = value; }
        }

        public Color AtmospherColor
        {
            get { return atmospherColor; }
            set { atmospherColor = value; }
        }
        public Planet(string name,string strImage, float diameter, float velocity, Orbit orbit)
            : base(strImage, diameter, velocity, orbit)
        {
            Name = name;
            initAtmosphereAndLand();
            
            patternId = Constant.random.Next(NPLANET);
            //patternId = 0;
            patternRotate = (float)(Constant.random.NextDouble() * Math.PI);
            viewLayer = new DetailPlanetView(this);
        }


        protected virtual void initAtmosphereAndLand()
        {
        }

        public void addSubPlanet(Moon moon)
        {
            Moons.Add(moon);
            moon.PlanetOwner = this;
        }
        public void AddUnit(Unit unit)
        {
            Units.Add(unit);

            //TODO: set visible on view.
            //viewLayer.AddChild(unit);
        }
        public void RemoveUnit(Unit unit)
        {
            Units.Remove(unit);
            viewLayer.RemoveChild(unit);
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            foreach (Moon moon in Moons)
            {
                moon.Update(gameTime);
            }
        }

        public override void UpdateInfo(GameTime gameTime)
        {
            base.UpdateInfo(gameTime);
            foreach (Moon moon in Moons)
            {
                moon.UpdateInfo(gameTime);
            }
            foreach (Unit unit in _units)
            {
                unit.UpdateInfo(gameTime);
            }
        }

        public virtual float getIncreaseMoneyAmount()
        {
            return 0;
        }

        public bool isEnoughResourcesForProduce(PlanetObjectOwner planetObj)
        {
            if (TeamOwner != null)
            {
                if (planetObj.isEnoughResourcesForProduce(this))
                    return true;
            }
            return false;
        }

        public List<int> GetNumberOfUnitPerType()
        {
            List<int> result = new List<int>();
            for (int i = 0; i < 4; i++)
                result.Add(0);

            foreach (Unit unit in Units)
            {
                if (unit.GetType() == typeof(SpaceCraft))
                    result[0]++;
                if (unit.GetType() == typeof(AirCraft))
                    result[1]++;
                if (unit.GetType() == typeof(BomberCraft))
                    result[2]++;
            }
            if (_teamOwner != null)
            {
                foreach (MotherShip mothership in _teamOwner.MotherShips)
                {
                    if (mothership.PlanetOwner == this)
                        result[3]++;
                }
            }
            return result;
        }
    }
}
